# -*- coding: utf-8 -*-  
# @Title     : ST3字体主题设置
# @Date      : 2014.4.20
# @Author    : aaron(583643865@qq.com)
# @Version   : V1.0
		

import wx
import os
#加载配置文件模块
import FileRead
#加载主题配置
import ThemeConfig

#打开帮助文档的东西
import subprocess as sp

#对齐窗口，右上角对齐
def alignToRight(win):
		dw,dh=wx.GetDisplaySize().Get()
		w,h=win.GetSize().Get()
		x=dw-w
		#y=dh-h
		win.SetPosition(wx.Point(x,0))

#主窗口
class MainFrame(wx.Frame):
	def __init__(self):

		#安装目录
		self.installdir=""
		#字体设置
		self.fontdata={}
		#配置文件路径
		self.configfile=""


		wx.Frame.__init__(self,None,-1,"Config",size=(200,250))
		#创建面板
		panel=wx.Panel(self,-1)
		#创建菜单栏
		menuBar=wx.MenuBar()
		#创建菜单
		fileMenu=wx.Menu()  #加载配置文件
		helpMenu=wx.Menu()  #关于
		#添加菜单
		fileMenu.Append(1001,"Install Dir")
		fileMenu.Append(1002,"Exit")
		helpMenu.Append(2001,"Doc")
		helpMenu.Append(2002,"About")

		menuBar.Append(fileMenu,"File(F)")
		menuBar.Append(helpMenu,"Help(H)")

		#创建按钮
		self.fontButton=wx.Button(panel,-1,"Font",pos=(43,40),size=(100,40))
		self.themeButton=wx.Button(panel,-1,"Theme",pos=(43,100),size=(100,40))

		#布局管理
		#绑定事件
		self.Bind(wx.EVT_MENU,self.LoadConfig,id=1001)
		self.Bind(wx.EVT_MENU,self.About,id=2002)
		self.Bind(wx.EVT_MENU,self.Exit,id=1002)
		self.Bind(wx.EVT_MENU,self.Doc,id=2001)

		#按钮事件
		self.Bind(wx.EVT_BUTTON,self.FontConfig,self.fontButton)
		self.Bind(wx.EVT_BUTTON,self.ThemeConfig,self.themeButton)
		#添加菜单栏到窗口中
		self.SetMenuBar(menuBar)

	#加载安装目录
	def LoadConfig(self,event):
		dlg=wx.DirDialog(self,"Choose Install Dir",style=wx.DD_DEFAULT_STYLE)
		if dlg.ShowModal()==wx.ID_OK:
			self.installdir=dlg.GetPath()
			self.configfile=self.installdir+"\\Data\\Packages\\User\\Preferences.sublime-settings"
		dlg.Destroy()
		#print self.installdir

	#字体设置
	def FontConfig(self,event):
		#找到配置文件
		if self.fileOpenError(self.configfile):
			
			data=wx.FontData()
			dlg=wx.FontDialog(self,data)
			if dlg.ShowModal()==wx.ID_OK:
				data=dlg.GetFontData()
				font=data.GetChosenFont()
				#设置字体的字典对象
				self.fontdata["font_face"]=font.GetFaceName()
				self.fontdata["font_size"]=font.GetPointSize()
				
				#调用写的配置文件读取模块
				fontSet=FileRead.PreferenceRead(self.configfile)
				#设置字体
				fontSet.setFont(self.fontdata)
			else:
				pass
		#如果找不到字体文件，提示出错！
		else:
			wx.MessageBox(u"Please Load The RIGHT installnation Directory!")


	#主题配置
	def ThemeConfig(self,event):
		if self.fileOpenError(self.configfile):
			theme=ThemeConfig.ThemeDialog(self.installdir)
			result=theme.ShowModal()
			theme.Destroy()
		else:
			wx.MessageBox(u"Please Load The RIGHT installnation Directory!")
			

	#配置文件找不到判断
	def fileOpenError(self,path):
		try :
			open(path,'r')
			return True
		except IOError:
			return False

	#退出程序
	def Exit(self,event):
		self.Close(True)

	#帮助文档
	def Doc(self,event):
		program='notepad.exe'
		f='help.txt'
		try:
			sp.Popen([program,f])
		except:
			pass

	#关于作者
	def About(self,event):
		wx.MessageBox("Made By Aaron!")



if __name__ == '__main__':
	app=wx.App()
	frame=MainFrame()
	alignToRight(frame)
	frame.Show(True)
	app.MainLoop()
